package controllers

import (
	"Department/models"
	beego "github.com/beego/beego/v2/server/web"
	"time"
)

type DepartController struct {
	beego.Controller
}

func (c *DepartController) Show() {
	id, _ := c.GetInt("id")
	var Deparment models.Department
	err := models.DB.Where("id = ? ", id).First(&Deparment).Error
	if err != nil {
		c.Data["json"] = DataMsg{
			Code: 500,
			Msg:  "详情展示失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = DataMsg{
			Code: 200,
			Msg:  "详情展示成功",
			Data: Deparment,
		}
		c.ServeJSON()
	}
}
func (c *DepartController) Update() {
	depart := c.GetString("depart")
	num, _ := c.GetInt("num")
	man := c.GetString("man")
	per := c.GetString("per")
	phone := c.GetString("phone")
	times := time.Now()
	id, _ := c.GetInt("Id")
	depar := models.Department{
		Depart: depart,
		Num:    num,
		Man:    man,
		Per:    per,
		Phone:  phone,
		Times:  times,
	}
	err := models.DB.Where("id = ?", id).Updates(&depar).Error
	if err != nil {
		c.Data["json"] = DataMsg{
			Code: 500,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = DataMsg{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		c.ServeJSON()
	}

}
func (c *DepartController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(models.Department{Id: id}).Error
	if err != nil {
		c.Data["json"] = DataMsg{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = DataMsg{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
